package com.example.kekewaimaibao.core.controller;

import com.baomidou.mybatisplus.core.toolkit.AES;
import com.example.kekewaimaibao.core.constant.RestConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: xuxiang
 * @date: 2022/3/8 9:28 星期二
 * @description: 测试使用AES + HEX数据库加解密算法
 * @version: 1.0
 */

@Slf4j
@RestController
@RequestMapping(EncryptController.PATH)
public class EncryptController {

    public static final String PATH = RestConstant.VERSION_V1 + "/core";
//    @GetMapping("/get-key")
    public String getKey() {
        String randomKey = AES.generateRandomKey();
        return randomKey;
        // 2d7186c8eb486cc2
    }

//    @GetMapping("/get-encrypt")
    public Map<String, Object> getEncrypt() {
        String randomKey = "2d7186c8eb486cc2";
        String url = "jdbc:mysql://localhost:3306/kekewaimaibao?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true";
        String username = "root";
        String password = "527757";
        String aesUrl = AES.encrypt(url, randomKey);
        String aesUsername = AES.encrypt(username, randomKey);
        String aesPassword = AES.encrypt(password, randomKey);
        Map<String, Object> map = new HashMap<>(3);
        map.put("url", aesUrl);
        map.put("username", aesUsername);
        map.put("password", aesPassword);
        log.info("EncryptMap ===> {}", map.toString());
        return map;
    }

//    @GetMapping("/get-decrypt")
    public Map<String, Object> getDecrypt() {
        String randomKey = "2d7186c8eb486cc2";
        String aesUrl = "ubB7aiGiQBONcn0BvI64RDSJdTAPPRRdKHRQXhc/+AuWK3q6K07L6QuEuH1yTKpTDrmIr7f8CUKzOqqUmweSgf/R1G5ORXKL0RMZ3MPlhOa7UU5Iyd+k11OnSJ5kkxVRsiayRArOF1UolA0zBy/5s/L7QZU5zMeTtkSErx3Sfjm4z7qbCOA5hlTuPr9Syl5f";
        String aesUsername = "23s3WsGrJdFJrLPF/wtwWw==";
        String aesPassword = "jhoZEhYNjJtJ778YdiOM/g==";
        String url = AES.decrypt(aesUrl, randomKey);
        String username = AES.decrypt(aesUsername, randomKey);
        String password = AES.decrypt(aesPassword, randomKey);
        Map<String, Object> map = new HashMap<>(3);
        map.put("url", url);
        map.put("username", username);
        map.put("password", password);
        log.info("EncryptMap ===> {}", map.toString());
        return map;
    }


}
